<template>
  <el-dialog
    :title="title"
    width="600px"
    :visible.sync="isShow"
    center
    @close="closeEvent"
  >
    <el-form label-width="80px" :model="from" :rules="rules" ref="from">
      <el-form-item prop="username" label="用户名">
        <el-input v-model.trim="from.username"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model.trim="from.email"></el-input>
      </el-form-item>
      <el-form-item prop="phone" label="电话">
        <el-input v-model.trim="from.phone"></el-input>
      </el-form-item>
      <el-form-item prop="role_id" label="角色">
        <el-select v-model="from.role_id" placeholder="请选择">
          <el-option
            v-for="item in employeesData.role"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="status" label="状态">
        <el-select v-model="from.status" placeholder="请选择">
          <el-option
            v-for="item in employeesData.status"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="remark" label="用户备注">
        <el-input v-model.trim="from.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="isShow = false">取消</el-button>
      <el-button type="primary" @click="submit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { UserAdd, UserEdit } from '@/api/cyy'
import employeesData from '@/api/constant/employess'
export default {
  computed: {
    title () {
      if (this.mode === 'add') {
        return '新增角色'
      }
      return '编辑角色'
    }
  },
  name: 'userListAddComponent',
  data () {
    return {
      isShow: false,
      employeesData: employeesData,
      mode: 'add',
      from: {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //	否	string	备注
      },
      rules: {
        username: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 2,
            max: 10,
            message: '请正确输入姓名,要求2-10字符',
            trigger: 'change'
          }
        ],
        email: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
            message: '请正确输入email',
            trigger: 'change'
          }
        ],
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请正确输入手机号',
            trigger: 'change'
          }
        ],
        role_id: [{ required: true, message: '必填', trigger: 'change' }],
        status: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    //确认按钮
    submit () {
      this.$refs.from.validate(async result => {
        console.log(result)
        if (result) {
          if (this.mode === 'add') {
            const res = await UserAdd(this.from)
            //提示
            if (res.data.code === 200) {
              this.$message.success('新增成功')
            }
          } else if (this.mode === 'edit') {
            await UserEdit(this.from)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          //刷新数据
          this.$emit('getData')
        }
      })
    },
    //清空数据
    closeEvent () {
      // 重置表单清除表单验证
      this.$refs.from.resetFields()
      this.from = {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //	否	string	备注
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog__header {
  color: #ffff;
  height: 30px;
  background: linear-gradient(90deg, #01c5fa, #1394fa);
  .el-dialog__title {
    height: 53px;
    color: #ffff;
    font-size: 16px;
  }
  .el-icon-close:before {
    color: #ffff;
  }
}

//
</style>
